package com.ol.edu.server.service.Authority.impl;


import com.ol.edu.server.entity.*;
import com.ol.edu.server.mapper.ResourceCategoryMapper;
import com.ol.edu.server.mapper.RoleMapper;
import com.ol.edu.server.mapper.RoleMenuMapper;
import com.ol.edu.server.mapper.RoleResourceMapper;
import com.ol.edu.server.service.Authority.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Roger
 * @date 2020/9/8 20:37
 * @description
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /*
        查询菜单列表
     */
    @Override
    public List<Role> findAllRole(Role role) {
        List<Role> roleList = roleMapper.findAllRole(role);
        return roleList;
    }
    /*
        添加角色
     */
    @Override
    public void saveRole(Role role) {
        roleMapper.saveRole(role);
    }
    /*
        修改角色
     */
    @Override
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
    }
    /*
        查询所有菜单列表
     */
    @Override
    public List<Menu> findAllMenu() {
        // -1表示是父菜单
        return roleMapper.findAllMenu(-1);
    }
    /*
        根据角色信息查询关联菜单
     */
    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {
        return roleMapper.findMenuByRoleId(roleId);
    }

    @Autowired
    private RoleMenuMapper roleMenuMapper;
    /*
        为角色分配菜单列表
     */
    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {
        Integer roleId = roleMenuVo.getRoleId();
        //先把原来存在的数据删掉
        roleMenuMapper.deleteMenuIdByRoleId(roleId);
        //再重新插入新的数据
        List<Integer> menuIdList = roleMenuVo.getMenuIdList();
        Date date = new Date();
        for (Integer menuId : menuIdList) {
            //插入菜单数据
            roleMenuMapper.insert(roleId,menuId);
        }

    }
    /*
        删除角色
     */
    @Override
    public void deleteRole(Integer id) {
        //先删除该角色拥有的菜单数据
        roleMenuMapper.deleteMenuIdByRoleId(id);
        //再删除角色数据
        roleMapper.deleteByRoleId(id);
    }

    @Autowired
    private ResourceCategoryMapper resourceCategoryMapper;
    /*
        获取当前角色拥有的资源信息
     */
    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer roleId) {
        List<ResourceCategory> resourceCategoryList = resourceCategoryMapper.findResourceListByRoleId(roleId);
        return resourceCategoryList;
    }

    @Autowired
    private RoleResourceMapper roleResourceMapper;
    /*
        为角色分配菜单
     */
    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {
        Integer roleId = roleResourceVo.getRoleId();
        //根据角色ID删除原来的关系数据
        roleResourceMapper.delete(roleId);
        List<Integer> resourceIdList = roleResourceVo.getResourceIdList();
        //逐条插入数据到表中
        for (Integer resourceId : resourceIdList) {
            roleResourceMapper.insert(roleId, resourceId);
        }
    }
}
